package cn.semcorp.zhuhaizj.repo.impl;

import cn.semcorp.zhuhaizj.mapper.TrainingMapper;
import cn.semcorp.zhuhaizj.pojo.dto.FlowerUserDTO;
import cn.semcorp.zhuhaizj.pojo.vo.SchemaVO;
import cn.semcorp.zhuhaizj.pojo.vo.WorkshopVO;
import cn.semcorp.zhuhaizj.repo.IRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static cn.semcorp.zhuhaizj.common.Common.icon2;

/**
 * 处理品牌缓存的数据访问实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Repository
public class RedisRepositoryImpl implements IRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    public RedisRepositoryImpl() {
        log.info("创建处理缓存的数据访问对象：SchemaRedisRepositoryImpl");
    }
    public static ListOperations<String, Serializable> ops ;

    @Override
    public void save(SchemaVO SchemaVO) {
        redisTemplate.opsForValue().set(SCHEMA_LIST_KEY, SchemaVO);
    }

    @Override
    public SchemaVO get() {
        Serializable serializable = redisTemplate
                .opsForValue().get(SCHEMA_LIST_KEY);
        SchemaVO schemaVO = null;
        if (serializable != null) {
            if (serializable instanceof SchemaVO) {
                schemaVO = (SchemaVO) serializable;
            }
        }
        return schemaVO;
    }

    @Override
    public String getCountPeopleInformation() {
        ops = redisTemplate.opsForList();
        List<Serializable> str = ops.range(INFORMATION_ITEM_KEY,0,-1);
        assert str != null;
        Serializable str0 = str.get(0);
        return ((String) str0);
    }

    @Override
    public void deleteSchema() {
        redisTemplate.delete(SCHEMA_LIST_KEY);
    }

    @Override
    public void deleteFlower() {
        redisTemplate.delete(OPERATOR_ITEM_KEY);
        redisTemplate.delete(MAIN_ITEM_KEY);
        redisTemplate.delete(MONITOR_ITEM_KEY);
    }

    @Override
    public void deleteFlowerList() {
        redisTemplate.delete(FLOWERS_ITEM_KEY);
    }

    @Override
    public void deleteCoatingWorkshop() {
        redisTemplate.delete(COATING_WORKSHOP_ITEM_KEY);
    }

    @Override
    public void deleteCountStudy() {
        redisTemplate.delete(STUDY_ITEM_KEY);
    }

    @Autowired
    private TrainingMapper tmpMapper;
    /**
     * 缓存操作工
     * @param
     * @return
     */
    @Override
    public void saveFlowersOperator(List<String> list) {
        if (tmpMapper.selectFlowerListNameByOperator(icon2)){
            ListOperations<String, Serializable> ops = redisTemplate.opsForList();
            for (String fo : list) {
                ops.rightPush(OPERATOR_ITEM_KEY, fo);
            }
        }
    }
    /**
     * 缓存主操
     * @param
     * @return
     */
    @Override
    public void saveFlowersMain(List<String> list) {
        if (tmpMapper.selectFlowerListNameByMain(icon2)){
            ListOperations<String, Serializable> ops = redisTemplate.opsForList();
            for (String fo : list) {
                ops.rightPush(MAIN_ITEM_KEY, fo);
            }
        }
    }

    /**
     * 缓存班长
     * @param
     * @return
     */
    @Override
    public void saveFlowersMonitor(List<String> list) {
        if (tmpMapper.selectFlowerListNameByMonitor(icon2)){
            ListOperations<String, Serializable> ops = redisTemplate.opsForList();
            for (String fo : list) {
                ops.rightPush(MONITOR_ITEM_KEY, fo);
            }
        }
    }



    /**
     * huancu
     * @param list
     */
    @Override
    public void saveFlowersList(List<FlowerUserDTO> list) {
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (FlowerUserDTO fo : list) {
            ops.rightPush(FLOWERS_ITEM_KEY, fo);
        }
    }

    /**
     * 存入原料查询车间
     * @param list
     */
    @Override
    public void saveMaterialWorkshopList(List<String> list) {
        ops = redisTemplate.opsForList();
        for (String fo : list) {
            ops.rightPush(MATERIAL_ITEM_KEY, fo);
        }
    }

    @Override
    public void saveCoatingWorkshop(List<WorkshopVO> list) {
        ops = redisTemplate.opsForList();
        for (WorkshopVO fo : list) {
            ops.rightPush(COATING_WORKSHOP_ITEM_KEY, fo);
        }
    }

    public void saveCountPeopleInformation(String str){
        ListOperations<String, Serializable> ops1 = redisTemplate.opsForList();
        ops1.rightPush(INFORMATION_ITEM_KEY, str);
    }

    @Override
    public void saveCountStudy(List<String> list) {
        ListOperations<String, Serializable> ops1 = redisTemplate.opsForList();
        for (String fo : list) {
            ops1.rightPush(STUDY_ITEM_KEY, fo);
        }
    }


    /**
     * 从redis中获取postclass类型人员
     * @param postClass
     * @return
     */
    @Override
    public List<String> getFlowers(String postClass) {
        ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(postClass, 0, -1);
        List<String> recordDTOS = new ArrayList<>();
        for (Serializable item : list) {
            recordDTOS.add((String) item);
        }
        return recordDTOS;
    }


    @Override
    public List<WorkshopVO> getCoatingWorkshop() {
        ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(COATING_WORKSHOP_ITEM_KEY, 0, -1);
        List<WorkshopVO> recordDTOS = new ArrayList<>();
        for (Serializable item : list) {
            recordDTOS.add((WorkshopVO) item);
        }
        return recordDTOS;
    }

    @Override
    public List<String> getCountStudy() {
        ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(STUDY_ITEM_KEY, 0, -1);
        List<String> recordDTOS = new ArrayList<>();
        for (Serializable item : list) {
            recordDTOS.add((String) item);
        }
        return recordDTOS;
    }

    public List<FlowerUserDTO> getFlowersList() {
        ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(FLOWERS_ITEM_KEY, 0, -1);
        List<FlowerUserDTO> recordDTOS = new ArrayList<>();
        for (Serializable item : list) {
            recordDTOS.add((FlowerUserDTO) item);
        }
        return recordDTOS;
    }

    /**
     * 获取原料查询的车间
     * @return
     */
    @Override
    public List<String> getMaterialWorkshopList() {
        ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(MATERIAL_ITEM_KEY, 0, -1);
        List<String> recordDTOS = new ArrayList<>();
        for (Serializable item : list) {
            recordDTOS.add((String) item);
        }
        return recordDTOS;
    }

}
